package com.shd.edanpei.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.base.BaseIPresenter;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.dialog.DialogUtil;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.bean.CustomerBean;
import com.shd.edanpei.bean.PicBean;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.enums.OrderTypeEnum;
import com.shd.edanpei.util.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/9/21.
 * 新建订单 步骤
 */

@SuppressWarnings("ALL")
public class CreateStepActivity extends BaseActivity {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.ll_step2)
    LinearLayout step2;
    @BindView(R.id.ll_step3)
    LinearLayout step3;
    @BindView(R.id.iv_step1)
    ImageView ivStep1;
    @BindView(R.id.iv_step2)
    ImageView ivStep2;
    @BindView(R.id.iv_step3)
    ImageView ivStep3;

    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_idcard)
    TextView tvIdcard;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_money2)
    TextView tvMoney2;
    @BindView(R.id.tv_upload_status)
    TextView tvUploadStatus;


    private Bundle bundle = new Bundle();
    private CustomerBean customerBean;
    //订单类型
    private int orderType;


    //客户个人信息是否完善
    private boolean isDoneCustomerInfo = false;
    //订单所需材料是否完善
    private boolean isDoneNeedData = false;

//    //客户id
//    private Integer customerID;
//    //订单id
//    private Integer orderID;


    @Override
    protected int setLayout() {
        return R.layout.activity_createstep;
    }

    @Override
    protected BaseIPresenter bindPresenter() {
        return null;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);

        Bundle extras = getIntent().getExtras();
        orderType = extras.getInt(Constant.ORDER_TYPE);
        customerBean = extras.getParcelable(Constant.DATA);

        bundle.putInt(Constant.ORDER_TYPE, orderType);
        headline.setText(OrderTypeEnum.getValue(orderType));
    }

    @Override
    protected void initListener() {

    }

    @OnClick(R.id.iv_headline_back)
    void onBack() {
        DialogUtil.infoDialog(this,
                "温馨提示",
                "确定放弃订单创建吗？放弃后所有已填数据将清空",
                "取消",
                "确定",
                new DialogUtil.SureListener() {
                    @Override
                    public void sure() {
                        DaoManager.getInstance().setIdCardRealName(null);
                        finish();
                    }
                });
    }

    @OnClick(R.id.ll_step1)
    void onCustomerInfo() {
        bundle.putParcelable(Constant.DATA, customerBean);
        if (customerBean.getIdCardNo() != null) {
            bundle.putInt(Constant.FROM, CustomerInfoActivity.NEW_INFO);
            Intent intent=new Intent();
            intent.setClass(CreateStepActivity.this,CustomerInfoActivity.class);
            intent.putExtras(bundle);
            startActivityForResult( intent,CustomerInfoActivity.RESULT);
        } else {
            bundle.putInt(Constant.FROM, CertificationActivity.FROM_CREATE);
            startActivity(CertificationActivity.class, bundle);
        }
    }

    @OnClick(R.id.ll_step2)
    void onCustomerData() {
        if (!isDoneCustomerInfo) {
            ToastUtil.showToast("客户个人信息未完善");
            return;
        }
        bundle.putParcelable(Constant.DATA, customerBean);
        if (orderType == OrderTypeEnum.car.getKey()) {

            startActivity(CarNeedDataActivity1.class, bundle);

        } else if (orderType == OrderTypeEnum.house.getKey()) {

            bundle.putParcelable("customerBean",customerBean);
            startActivity(HouseNeedDataActivity1.class, bundle);

        }else if(orderType==OrderTypeEnum.personal.getKey()){

            startActivity(PersionalActivity.class, bundle);

        }else if(orderType==OrderTypeEnum.enterprise.getKey()){
            startActivity(PersionalActivity.class, bundle);
        }
    }

    @OnClick(R.id.ll_step3)
    void onCustomerFile() {
        if (!isDoneCustomerInfo) {
            ToastUtil.showToast("客户个人信息未完善");
            return;
        }
        if (!isDoneNeedData) {
            ToastUtil.showToast("订单所需材料未完善");
            return;
        }

        bundle.putInt(Constant.ORDER_TYPE, orderType);
        bundle.putInt(Constant.TYPE, Constant.EDIT);
        bundle.putParcelable(Constant.DATA, customerBean);
        bundle.putParcelable("images", picBean);
        if(orderType==OrderTypeEnum.personal.getKey()||orderType==OrderTypeEnum.enterprise.getKey()){

            startActivity(FileListPersonalAndEnterpActivity.class, bundle);
        }else if(orderType==OrderTypeEnum.house.getKey()||orderType==OrderTypeEnum.car.getKey()) {

            startActivity(FileListActivity.class, bundle);
        }

    }

    @Subscribe(code = RxBusCode.CUSTOMER_INFO_SUBMIT_SUCCESS)
    public void doneCustomerInfo(CustomerBean customerBean) {
        isDoneCustomerInfo = true;
        this.customerBean = customerBean;
        ivStep2.setImageResource(R.drawable.ic_step2);
        step2.setBackgroundColor(getResources().getColor(R.color.white));
        tvName.setText(customerBean.getName());
        tvIdcard.setText("**** **** ****"+customerBean.getIdCardNo().substring(14,customerBean.getIdCardNo().length()));
    }

    @Subscribe(code = RxBusCode.NEED_DATA_SUBMIT_SUCCESS)
    public void doneNeedData(CustomerBean customerBean) {
        isDoneNeedData = true;
        this.customerBean = customerBean;
        ivStep3.setImageResource(R.drawable.ic_step3);
        step3.setBackgroundColor(getResources().getColor(R.color.white));
        tvMoney.setText(customerBean.getApplyAmount());
        tvMoney2.setText(customerBean.getApplyInterest());
    }

    @Subscribe(code = RxBusCode.ORDER_CREATE_SUCCESS)
    public void isUpdata() {
        this.customerBean = customerBean;
        tvUploadStatus.setVisibility(View.VISIBLE);
    }
    private PicBean picBean = new PicBean();

    @Subscribe(code = RxBusCode.SAVE_FILE_DATA)
    public void saveFileData(PicBean picBean) {
        this.picBean = picBean;
    }

    @Subscribe(code = RxBusCode.ORDER_CREATE_SUCCESS)
    public void orderCreateSuccess() {
        finish();
    }

    @Override
    protected void onDestroy() {
        DaoManager.getInstance().setIdCardRealName(null);
        RxBus.get().unRegister(this);
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBack();
        }
        return false;
    }

    @Override
    public void onActivityReenter(int resultCode, Intent data) {
        super.onActivityReenter(resultCode, data);
//        if(resultCode==CustomerInfoActivity.RESULT){
//
//        }
    }

}
